﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingWorkshop.BLL;
using TrainingWorkshop.Model;

namespace Presentation
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Logical log = new Logical();
                log.ChooseTable("Department",  true);
                DropDownListDepartment.Items.Clear();
                DropDownListDepartment.Items.Capacity = log.Dep.Count;
                DropDownListDepartment.Items.Insert(0, "Please Select");
               
                foreach (var a in log.Dep)
                {
                    ListItem b = new ListItem(a.DepartmentName);
                    if (!DropDownListDepartment.Items.Contains(b))
                    {
                       
                        DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);
                    }

                }

                string id = Request["ID"];
                int TeacherID = Convert.ToInt32(id);
               
                log.ChooseTable("Teacher", false, TeacherID);

                
                foreach(var a in log.Tea)
                {
                   TeacherNameBox.Text = a.TeacherName;
                   Logical l= new Logical();
                    l.ChooseTable("Department",false,a.DepartMent.DepartmentId);
                    foreach(var b in l.Dep)
                    {
                        DropDownListDepartment.SelectedValue=b.DepartmentName;//figure how to get departmentname through dropdownlist
                    }
                   
                   if (a.TeacherGender.Trim().Equals("Male"))
                   {
                       RadioButtonList1.SelectedIndex = 0;
                   }
                   else
                    {
                        RadioButtonList1.SelectedIndex = 1;
                    }
                }
                
                
            }
            
        }

        protected void Button_Update_Click(object sender, EventArgs e) //return to teacher list page
        {
            string id = Request["ID"];
            int TeacherID = Convert.ToInt32(id);
            string str=null;
            Logical l = new Logical();
            l.ChooseTable("Department", false, DropDownListDepartment.SelectedIndex);
            
            Department myDep = new Department(); 
            foreach(var b in l.Dep)
            {
            
                myDep = b;
            }

            if(0==RadioButtonList1.SelectedIndex)
            {
                str="Male";
            }
            else
            {
                str="Female";
            }
            Teacher a =new Teacher
            {
                TeacherId = TeacherID,
                 TeacherName = TeacherNameBox.Text,
                 TeacherGender = str,
                 DepartMent = myDep
            };

            Logical log = new Logical();
            
            if (log.OperationChoose("Update", false, a))
                Response.Write("<script>alert('Update Succeed');window.location.href='TeacherList.aspx';</script>");
            else
                Response.Write("<script>alert('Update Failed');window.location.href='TeacherList.aspx';</script>");
            Response.End();

        }
    }
}